package services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import models.DrSecretExample;
import models.DrVCase;
import models.SystemLog;
import models.SystemOperator;
import models.SystemOperatorExample;
import models.mapper.DrSecretMapper;
import models.mapper.DrVCaseMapper;
import models.mapper.SystemLogMapper;
import models.mapper.SystemOperatorMapper;
import play.libs.Codec;
import utils.PageUtil;
import vos.CaseSearchVo;
import vos.SystemOperatorVo;

import com.aton.db.SessionFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 操作员服务类
 * @author wangshaoming
 *
 */
public class OperatorService {
	/**
	 * 根据id查询操作员信息
	 * @param systemOperatorid
	 * @return
	 */
	public static SystemOperator getById(String systemOperatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			SystemOperatorMapper mapper=ss.getMapper(SystemOperatorMapper.class);
			return mapper.selectByPrimaryKey(systemOperatorid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 修改密码
	 * @param pass 新密码
	 * @param operatorid 操作员id
	 * @param oper 操作员
	 * @param ip ip
	 * @return
	 */
	public static boolean updatePass(String operatorid, String pass, SystemOperator oper, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			//修改密码
			SystemOperatorMapper mapper=ss.getMapper(SystemOperatorMapper.class);
			SystemOperator newoper=new SystemOperator();
			newoper.setOperatorid(operatorid);
			newoper.setOpreaUser(oper.getOperatorname());
			newoper.setModifytime(new Date());
			newoper.setPassword(pass);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),oper.getOperatorid(),ip,"修改密码","修改"+operatorid+"的密码",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			mapper.updateByPrimaryKeySelective(newoper);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 获取用户列表
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param vo
	 * @return
	 */
	public static PageInfo<SystemOperatorVo> getList(int page, int rows, String sort,
			String order, String searchText) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			SystemOperatorMapper mapper=ss.getMapper(SystemOperatorMapper.class);
			PageHelper.startPage(page, rows,PageUtil.joinSortFieldOrder(sort, order));
			return new PageInfo<SystemOperatorVo>(mapper.findCalssByName(searchText));
			
		}finally{
			ss.close();
		}
	}
	/**
	 * 验证用户编号是否可用
	 * @param value
	 * @return
	 */
	public static boolean checkCode(String value) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			SystemOperatorMapper mapper = ss.getMapper(SystemOperatorMapper.class);
	        String operator= mapper.checkCode(value);
	        if(StringUtils.isEmpty(operator)){
	        	return false;
	        }
	        else{
	        	return true;
	        }
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存用户信息
	 * @param operator
	 * @param loginUser
	 * @param ip
	 * @return
	 */
	public static boolean saveOperator(SystemOperator operator,
			SystemOperator loginUser, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			SystemOperatorMapper mapper = ss.getMapper(SystemOperatorMapper.class);
			String salt=String.valueOf(Math.random()).substring(2, 8);
			operator.setSalt(salt);
			operator.setCreatetime(new Date());
			operator.setOpreaUser(loginUser.getOperatorid());
			String password=Codec.hexMD5((operator.getPassword()+salt));
			operator.setPassword(password);
			mapper.insert(operator);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"添加用户","添加用户"+operator.getOperatorname(),new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量更改操作员状态
	 * @param listIds 用户id
	 * @param ip 操作人ip
	 * @param loginUser 操作员信息
	 * @param moudel 
	 * @param bigDecimal 
	 * @return 
	 */
	public static boolean betchDisable(List<String> listIds, String ip,
			SystemOperator loginUser, String moudel, BigDecimal bigDecimal) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			SystemOperatorMapper mapper= ss.getMapper(SystemOperatorMapper.class);
			SystemOperatorExample example=new SystemOperatorExample();
			SystemOperatorExample.Criteria c=example.createCriteria();
			c.andOperatoridIn(listIds);
			SystemOperator oper=new SystemOperator();
			oper.setStatus(bigDecimal);
			mapper.updateByExampleSelective(oper, example);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"更改操作员状态",moudel,new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新操作员信息
	 * @param operator
	 * @param loginUser
	 * @param ip
	 * @return
	 */
	public static boolean updateOperator(SystemOperator operator,
			SystemOperator loginUser, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			SystemOperatorMapper mapper= ss.getMapper(SystemOperatorMapper.class);
			operator.setModifytime(new Date());
			mapper.updateByPrimaryKeySelective(operator);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"修改用户","修改用户"+operator.getOperatorid(),new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 获取操作员的密级等级
	 * @param operatorid
	 * @return
	 */
	public static long getLevelById(String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			SystemOperatorMapper mapper= ss.getMapper(SystemOperatorMapper.class);
			return mapper.getLevelById(operatorid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取最大密级的id
	 * @return
	 */
	public static String getMaxLevel() {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DrSecretMapper mapper=ss.getMapper(DrSecretMapper.class);
			List<String> ids=mapper.getMaxLevel();
			if(ids!=null&&ids.size()>0){
				return ids.get(0);
			}
			else{
				return "";
			}
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取最小密级的id
	 * @return
	 */
	public static String getMinLevel() {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			DrSecretMapper mapper=ss.getMapper(DrSecretMapper.class);
			List<String> ids=mapper.getMinLevel();
			if(ids!=null&&ids.size()>0){
				return ids.get(0);
			}
			else{
				return "";
			}
		}finally{
			ss.close();
		}
	}
}
